package com.pig4cloud.pig.environment.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.environment.entity.EnterpriseEntity;
import com.pig4cloud.pig.environment.service.EnterpriseService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 企业
 *
 * @author joyql
 * @date 2024-01-16 15:04:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/enterprise" )
@Tag(description = "enterprise" , name = "企业管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EnterpriseController {

    private final  EnterpriseService enterpriseService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param enterprise 企业
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('environment_enterprise_view')" )
    public R getEnterprisePage(@ParameterObject Page page, @ParameterObject EnterpriseEntity enterprise) {
        LambdaQueryWrapper<EnterpriseEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(enterprise.getName()),EnterpriseEntity::getName,enterprise.getName());
		wrapper.eq(StrUtil.isNotBlank(enterprise.getCreditCode()),EnterpriseEntity::getCreditCode,enterprise.getCreditCode());
		wrapper.eq(StrUtil.isNotBlank(enterprise.getPollutionSourceCategory()),EnterpriseEntity::getPollutionSourceCategory,enterprise.getPollutionSourceCategory());
		wrapper.eq(StrUtil.isNotBlank(enterprise.getPollutionCategory()),EnterpriseEntity::getPollutionCategory,enterprise.getPollutionCategory());
		wrapper.eq(StrUtil.isNotBlank(enterprise.getLegalPersonContact()),EnterpriseEntity::getLegalPersonContact,enterprise.getLegalPersonContact());
		wrapper.likeRight(StrUtil.isNotBlank(enterprise.getRegionNo()), EnterpriseEntity::getRegionNo,enterprise.getRegionNo());
        return R.ok(enterpriseService.page(page, wrapper));
    }


    /**
     * 通过id查询企业
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('environment_enterprise_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(enterpriseService.getById(id));
    }

    /**
     * 新增企业
     * @param enterprise 企业
     * @return R
     */
    @Operation(summary = "新增企业" , description = "新增企业" )
    @SysLog("新增企业" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('environment_enterprise_add')" )
    public R save(@RequestBody EnterpriseEntity enterprise) {
        return R.ok(enterpriseService.save(enterprise));
    }

    /**
     * 修改企业
     * @param enterprise 企业
     * @return R
     */
    @Operation(summary = "修改企业" , description = "修改企业" )
    @SysLog("修改企业" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('environment_enterprise_edit')" )
    public R updateById(@RequestBody EnterpriseEntity enterprise) {
        return R.ok(enterpriseService.updateById(enterprise));
    }

    /**
     * 通过id删除企业
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除企业" , description = "通过id删除企业" )
    @SysLog("通过id删除企业" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('environment_enterprise_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(enterpriseService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param enterprise 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('environment_enterprise_export')" )
    public List<EnterpriseEntity> export(EnterpriseEntity enterprise,Long[] ids) {
        return enterpriseService.list(Wrappers.lambdaQuery(enterprise).in(ArrayUtil.isNotEmpty(ids), EnterpriseEntity::getId, ids));
    }
}